import fs from 'fs';
import { fromMarkdown } from 'mdast-util-from-markdown';

const readFileAsync = filePath => {
    return new Promise((resolve, reject) => {
        fs.readFile(filePath, 'utf-8', (err, data) => {
            if (err) {
                reject(err);
            } else {
                resolve(data);
            }
        });
    });
};

const writeFileAsync = (filePath, content) => {
    return new Promise((resolve, reject) => {
        fs.writeFile(filePath, content, 'utf-8', err => {
            if (err) {
                reject(err);
            } else {
                resolve();
            }
        });
    });
};

const convertMarkdownToJson = async (inputFile, outputFile) => {
    try {
        const markdown = await readFileAsync(inputFile);
        const tree = fromMarkdown(markdown);
        const json = JSON.stringify(tree, null, 2);
        await writeFileAsync(outputFile, json);
        console.log('File has been written successfully.');
    } catch (err) {
        console.error('An error occurred:', err);
    }
};

const inputFile = './CHANGELOG.md';
const outputFile = './output.json';

convertMarkdownToJson(inputFile, outputFile);
